<?php
namespace M\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function _initialize(){
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET');
        header('Access-Control-Allow-Methods:POST');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
    }

    /**用户注册
     * para:phonenumber,password,phonecode
     * return
     *  code
     *  msg
     */
    public function register(){
        $phonenumber=I('phonenumber');
        $password=I('password');
        $phonecode=I('phonecode');
        $time=date('YmdHis',time());
        $user=D('user');
        $code=M('code');

        $realCode=$code->where(array('phonenumber'=>$phonenumber))->field('code,time,status')->find();
        if(strtotime($realCode['time'])-time() <= 0)//过时失效
        {
            $code->where(array('phonenumber'=>$phonenumber))->save(array('status'=>'0'));
            $realCode['status']='0';
        }
        if($realCode['status']!='1')
        {
            $data=array(
                'code'=>'0',
                'msg'=>'验证码已失效'
            );
            echo json_encode($data,JSON_UNESCAPED_UNICODE);
            exit;
        }

        if(!$user->create(I('get.')))
        {
            $data=array(
                'code'=>'2',
                'msg'=>$user->getError()
            );
        }else{
            if($realCode['code']==$phonecode )
            {
                $info=array(
                    'id'=>md5($time),
                    'password'=>md5($password),
                    'phonenumber'=>$phonenumber,
                    'time'=>$time
                );
                M('user')->add($info);
                $code->where(array('phonenumber'=>$phonenumber))->save(array('status'=>'0'));
                $data=array(
                    'code'=>'1',
                    'msg'=>'用户注册成功'
                );
            }else{
                $data=array(
                    'code'=>'3',
                    'msg'=>'验证码错误'
                );
            }

        }
        echo json_encode($data,JSON_UNESCAPED_UNICODE);

    }

    /**登录
     * para:phonenumber,password
     * reuturn:
     *  code 1、成功 0、失败
     *  msg
     *  token
     *  user_id
     */
    public function login(){
        $phonenumber=I('phonenumber');
        $password=I('password');
        if(!empty($phonenumber) && !empty($password))
        {
            $result=M('user')->where(array('phonenumber'=>$phonenumber))->field('id,password')->find();
            if($result!='' && md5($password)==$result['password'])
            {
                $data=array(
                    'code'=>'1',
                    'msg'=>'登陆成功',
                    'token'=>'',
                    'user_id'=>$result['id']
                );
            }else{
                $data=array(
                    'code'=>'0',
                    'msg'=>'手机号码或者密码错误'
                );
            }
        }else{
            $data=array(
                'code'=>'2',
                'msg'=>'请输入完整信息'
            );
        }
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    /**忘记密码
     * para：phonenumber,phonecode, password
     * return:
     *  code
     *  msg
     *  
     */
    public function forgetPassword(){
        $phonenumber=I('phonenumber');
        $phonecode=I('phonecode');
        $password=I('password');
        $user=M('user');
        $code=M('code');

        $realCode=$code->where(array('phonenumber'=>$phonenumber))->field('code,time,status')->find();
        if(strtotime($realCode['time'])-time() <= 0)//过时失效
        {
            $code->where(array('phonenumber'=>$phonenumber))->save(array('status'=>'0'));
            $realCode['status']='0';
        }
        if($realCode['status']!='1')
        {
            $data=array(
                'code'=>'0',
                'msg'=>'验证码已失效'
            );
            echo json_encode($data,JSON_UNESCAPED_UNICODE);
            exit;
        }

        if(!empty($phonecode) && $realCode['code']==$phonecode)
        {
            $flag=$user->where(array('phonenumber'=>$phonenumber))->getField('id');
            if(!empty($phonenumber) && $flag!='')
            {
                if(!empty($password))
                {
                    $user->password=md5($password);
                    $user->where(array('phonenumber'=>$phonenumber))->save();
                    $code->where(array('phonenumber'=>$phonenumber))->save(array('status'=>'0'));
                    $data=array(
                        'code'=>'1',
                        'msg'=>'修改成功'
                    );
                }else{
                    $data=array(
                        'code'=>'2',
                        'msg'=>'密码不能为空'
                    );
                }
            }else{
                $data=array(
                    'code'=>'3',
                    'msg'=>'电话号码有误'
                );
            }

        }else{
            $data=array(
                'code'=>'0',
                'msg'=>'验证码输入错误'
            );
        }
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    /**修改密码
     * para:user_id
     * return:
     *  code
     *  msg
     */
    public function passwordUpdate(){
        $id=I('user_id');
        $phonenumber=I('phonenumber');
        $password=I('password');
        $newpassword=I('newpassword');
        $user=M('user');
        $result=$user->where(array('phonenumber'=>$phonenumber))->field('id,password')->find();
        if($result!='' && md5($password)==$result['password'])
        {
            $user->password=md5($newpassword);
            $flag=$user->where(array('phonenumber'=>$phonenumber))->save();
            if($flag!=false)
            {
                $data=array(
                    'code'=>'1',
                    'msg'=>'密码成功',
                );
            }else{
                $data=array(
                    'code'=>'2',
                    'msg'=>'新密码不能跟原密码相同',
                );
            }
        }else{
            $data=array(
                'code'=>'0',
                'msg'=>'原有密码或电话号码有误'
            );
        }
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

//    public function redis(){
//        S(array('type'=>'redis','expire'=>60));
//        S(array(
//                'type'=>'redis',
//                'host'=>'127.0.0.1',
//                'port'=>'6379',
//                'expire'=>60)
//        );
//        S('info','2321');
//        $redis = new \Redis();
//        $redis->connect('127.0.0.1',6379);
//        $redis->set('test','hello world!');
//        echo $redis->get("test");
//        echo $value=S('info');
//
//        $cache = S(array('type'=>'redis','prefix'=>'think_','expire'=>600));
//        $cache->name = 'value'; // 设置缓存
//        echo $value = $cache->name; // 获取缓存
//    }
//
//    public function test2(){
//        echo time()+60-time();
//        die;
//        $redis = new \Redis();
//        $redis->connect('localhost', '6379');
//        $redis->set('test','hao');
//        echo $redis->get('test');
//    }
}